﻿#include "db_api_.h"

#define LOCK	OAL_mutex_lock(&hDB->mutex)
#define UNLOCK	OAL_mutex_unlock(&hDB->mutex)

int db_init(db_cb cb, get_sd_status cb_get_sd_status, unsigned short codepage)
{
	DbCodePage = codepage;
	int ret = 0;
	if (cb_get_sd_status(0))
	{
		DWORD nclst;
		DWORD clust_size;
		ext_get_free_(&nclst,&clust_size);
		if((nclst * clust_size/2)< 32*1024) return -100;
	}
	if(hDB)
	{
		_db_free();
	}

	ret = _db_init(cb, cb_get_sd_status);
	if(ret)
	{
		_db_free();
	}
	return ret;
}

void db_free()
{
	_db_free();
}

int db_reset(void)
{
	return _db_reset();
}

int db_mount(void)
{
	int ret = 0;
	if(hDB == 0) return -1;
	LOCK;
	ret = _db_mount();
	UNLOCK;
	return ret;
}

int db_unmount(void)
{
	int ret = 0;
	if(hDB == 0) return -1;
	LOCK;
	ret = _db_unmount();
	UNLOCK;
	return ret;
}

int db_is_dirty(int module)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if ((module & MODULE_ALL) == 0) return -1;
	LOCK;
	ret = hDB->dirty & module;
	UNLOCK;
	return ret;
}
void db_set_dirty(int dirty)
{
	hDB->dirty = dirty;
}
int db_audio_build(const char * ignore_dir[])
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = audio_build(ignore_dir);
	save_content();
	UNLOCK;
	return ret;
}

int db_album_build(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = album_build();
	save_content();
	UNLOCK;
	return ret;
}

int db_artist_build(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = artist_build();
	save_content();
	UNLOCK;
	return ret;
}

int db_get_audio_count(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = hDB->audio_count;
	UNLOCK;
	return ret;
}

int db_get_album_count(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = hDB->album_count;
	UNLOCK;
	return ret;
}

int db_get_artist_count(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = hDB->artist_count;
	UNLOCK;
	return ret;
}

int db_get_audio_records(unsigned int offset, AUDIORECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_audio_records(offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_get_audio_name(unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_audio_name(offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_get_albums(unsigned int offset, RECID3ITEM* albums, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (albums == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_albums(offset, albums, cnt);
	UNLOCK;
	return ret;
}

int db_get_artists(unsigned int offset, RECID3ITEM* artists, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (artists == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_artists(offset, artists, cnt);
	UNLOCK;
	return ret;
}

int db_get_audio_count_of_album(RECID3ITEM album)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (album == 0) return -1;
	LOCK;
	ret = get_audio_count_of_album(album);
	UNLOCK;
	return ret;
}

int db_get_audio_count_of_artist(RECID3ITEM artist)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (artist == 0) return -1;
	LOCK;
	ret = get_audio_count_of_artist(artist);
	UNLOCK;
	return ret;
}

int db_get_audio_name_by_album(char* album, unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (album == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_audio_name_by_album(album, offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_get_audio_name_by_artist(char* artist, unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (artist == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_audio_name_by_artist(artist, offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_get_audio_record_by_album(char* album, unsigned int offset, AUDIORECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (album == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_audio_record_by_album(album, offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_get_audio_record_by_artist(char* artist, unsigned int offset, AUDIORECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (artist == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_audio_record_by_artist(artist, offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_video_build(const char * ignore_dir[])
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = video_build(ignore_dir);
	save_content();
	UNLOCK;
	return ret;
}

int db_get_video_count(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = hDB->video_count;
	UNLOCK;
	return ret;
}

int db_get_video_records(unsigned int offset, VIDEORECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_video_records(offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_get_video_name(unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_video_name(offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_image_build(const char * ignore_dir[])
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = image_build(ignore_dir);
	save_content();
	UNLOCK;
	return ret;
}

int db_get_image_count(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = hDB->image_count;
	UNLOCK;
	return ret;
}

int db_get_image_records(unsigned int offset, IMAGERECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_image_records(offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_get_image_name(unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_image_name(offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_ebook_build(const char * ignore_dir[])
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = ebook_build(ignore_dir);
	save_content();
	UNLOCK;
	return ret;
}

int db_get_ebook_count(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = hDB->ebook_count;
	UNLOCK;
	return ret;
}

int db_get_ebook_records(unsigned int offset, EBOOKRECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_ebook_records(offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_get_ebook_name(unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_ebook_name(offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_get_ebookSN(char* path, unsigned int* ebookSN)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if ((file_type(path) & MEDIA_HI_EBOOK) == 0) return -1;
	if (ebookSN == 0) return -1;
	LOCK;
	ret = get_ebookSN(path, ebookSN);
	UNLOCK;
	return ret;
}

int db_wave_build(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = wave_build();
	save_content();
	UNLOCK;
	return ret;
}

int db_get_wave_count(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = hDB->wave_count;
	UNLOCK;
	return ret;
}

int db_get_wave_records(unsigned int offset, WAVERECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_wave_records(offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_get_wave_name(unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	if (cnt > MAXRECORD) return -1;
	LOCK;
	ret = get_wave_name(offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_add_wave(char* path)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if ((file_type(path) & MEDIA_HI_WAVE) == 0) return -1;
	LOCK;
	ret = add_wave(path);
	save_content();
	UNLOCK;
	return ret;
}

int db_enter_folder(char* dir)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (dir == 0) return -1; 
	LOCK;
	ret = enter_folder(dir);
	UNLOCK;
	return ret;
}

int db_get_count_of_folder(char* dir)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (dir == 0) return -1;
	LOCK;
	ret = get_count_of_folder(dir);
	UNLOCK;
	return ret;
}

int db_get_folder_records(char* dir, unsigned int offset, FOLDERRECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (dir == 0) return -1;
	//if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	LOCK;
	ret = get_folder_records(dir, offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_build_fpl(char* dir)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (dir == 0) return -1;
	LOCK;
	ret = build_fpl(dir);
	UNLOCK;
	return ret;
}

int db_get_count_of_fpl(char* dir)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (dir == 0) return -1;
	LOCK;
	ret = get_count_of_fpl(dir);
	UNLOCK;
	return ret;
}

int db_get_fpl_records(char* dir, unsigned int offset, FOLDERRECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (dir == 0) return -1;
	//if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	LOCK;
	ret = get_fpl_records(dir, offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_get_fpl_name(char* dir, unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (dir == 0) return -1;
	//if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	LOCK;
	ret = get_fpl_name(dir, offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_get_fpl_index(char* path)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if(!is_archive(path))	return -1;
	LOCK;
	ret = get_fpl_index(path);
	UNLOCK;
	return ret;
}

int db_delete_file(char* path)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if(1!=is_archive(path))	return -1;
	LOCK;
	ret = delete_file(path);
	save_content();
	UNLOCK;
	return ret;
}

int db_delete_floder_file(char* path)
{

	FRESULT ret = ext_delete_tree(path);
	return ret;
}

int db_get_audio_id3(char* path, AUDIORECORD* record)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if(!is_archive(path))	return -1;
	if (record == 0) return -1;
	LOCK;
	ret = get_audio_id3(path, record);
	UNLOCK;
	return ret;
}

int db_like_build(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = like_build();
	save_content();
	UNLOCK;
	return ret;
}

int db_get_like_count(int like)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (like <= 0 || like > MAXLIKETYPE) return -1;
	LOCK;
	ret = hDB->like_count[like-1];
	UNLOCK;
	return ret;
}

int db_get_like_records(int like, unsigned int offset, LIKERECORD* records, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (like <= 0 || like > MAXLIKETYPE) return -1;
	if (offset > MAXRECORD) return -1;
	if (records == 0) return -1;
	if (cnt > MAXLIKERECORD) return -1;
	LOCK;
	ret = get_like_records(like-1, offset, records, cnt);
	UNLOCK;
	return ret;
}

int db_get_like_name(int like, unsigned int offset, RECUTF8NAME* fname, unsigned int cnt)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (like <= 0 || like > MAXLIKETYPE) return -1;
	if (offset > MAXRECORD) return -1;
	if (fname == 0) return -1;
	if (cnt > MAXLIKERECORD) return -1;
	LOCK;
	ret = get_like_name(like-1, offset, fname, cnt);
	UNLOCK;
	return ret;
}

int db_is_like(char* path, int* like)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if (like == 0) return -1;
	if((file_type(path) & (MEDIA_HI_AUDIO | MEDIA_HI_WAVE)) == 0)	return -1;
	if(!is_archive(path))	return -1;
	LOCK;
	ret = is_like(path, like);
	UNLOCK;
	return ret;
}

int db_set_like(char* path, int like)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if((file_type(path) & (MEDIA_HI_AUDIO | MEDIA_HI_WAVE)) == 0)	return -1;
	if (like == 0 || like < -MAXLIKETYPE || like > MAXLIKETYPE) return -1;
	LOCK;
	ret = set_like(path, like);
	save_content();
	UNLOCK;
	return ret;
}

int db_tag_build(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = tag_build();
	save_content();
	UNLOCK;
	return ret;
}

int db_get_tag_count(void)
{
	int ret = 0;
	if (hDB == 0) return -1;
	LOCK;
	ret = hDB->tag_count;
	UNLOCK;
	return ret;
}

int db_get_tag_record(char* path, TAGRECORD* record)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if(!is_archive(path))	return -1;
	if((file_type(path) & (MEDIA_HI_EBOOK|MEDIA_HI_AUDIO|MEDIA_HI_WAVE)) == 0)	return -1;
	if (record == 0) return -1;
	LOCK;
	ret = get_tag_record(path, record);
	UNLOCK;
	return ret;
}

int db_add_tag(char* path, unsigned int No, unsigned int tag)
{
	int ret = 0;
	if (hDB == 0) return -1;
	if (path == 0) return -1;
	if(!is_archive(path))	return -1;
	if((file_type(path) & (MEDIA_HI_EBOOK|MEDIA_HI_AUDIO|MEDIA_HI_WAVE)) == 0)	return -1;
	if (No >= MAXTAG) return -1; 
	LOCK;
	ret = add_tag(path, No, tag);
	save_content();
	UNLOCK;
	return ret;
}
